import { SidebarStatusEnum } from "@/enums/SidebarStatusEnum";
import { SizeEnum } from "@/enums/SizeEnum";
import defaultSettings from "@/settings";
import { defineStore } from "pinia";
// 导入 Element Plus 中英文语言包
import zhCn from "element-plus/es/locale/lang/zh-cn";
import en from "element-plus/es/locale/lang/en";
import { store } from "@/store";

export const useAppStore = defineStore("app", () => {
  // 布局大小
  const size = useStorage("size", defaultSettings.size);
  // 语言
  const language = useStorage("language", defaultSettings.language);

  const sidebarStatus = useStorage("sidebarStatus", SidebarStatusEnum.CLOSED);
  const sidebar = reactive({
    opened: sidebarStatus.value === SidebarStatusEnum.OPENED,
    withoutAnimation: false,
  });

  /**
   * 侧边栏开关
   */
  function toggleSideBar() {
    sidebar.opened = !sidebar.opened;
    sidebar.withoutAnimation = false;
    if (sidebar.opened) {
      sidebarStatus.value = SidebarStatusEnum.OPENED;
    } else {
      sidebarStatus.value = SidebarStatusEnum.CLOSED;
    }
  }

  /**
   * 切换大小
   * @param val
   */
  function changeSize(val: SizeEnum) {
    size.value = val;
  }

  /**
   * 根据语言标识读取对应的语言包
   */
  const locale = computed(() => {
    if (language?.value == "en") {
      return en;
    } else {
      return zhCn;
    }
  });
  /**
   * 切换语言
   *
   * @param val
   */
  function changeLanguage(val: string) {
    language.value = val;
  }

  return {
    size,
    sidebar,
    language,
    locale,
    changeSize,
    toggleSideBar,
    changeLanguage,
  };
});

/**
 * 用于在组件外部（如在Pinia Store 中）使用 Pinia 提供的 store 实例。
 * 官方文档解释了如何在组件外部使用 Pinia Store：
 * https://pinia.vuejs.org/core-concepts/outside-component-usage.html#using-a-store-outside-of-a-component
 */
export function useAppStoreHook() {
  return useAppStore(store);
}
